package com.wanxi.app.controller.api;

import com.wanxi.app.service.user.AddressService;
import com.wanxi.user.domain.dto.AddressDefaultDto;
import com.wanxi.user.domain.dto.AddressDto;
import com.wanxi.user.domain.entity.Address;
import com.wanxi.user.domain.result.UserResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:wgy
 * @Data：2022/1/12 10:14
 */
@RestController
@RequestMapping("/app")
@Api(tags = "收货地址")
public class AddressApi {

    @Resource
    private AddressService addressService;

    /**
     *查询用户收获地址
     *
     * @param userId  用户id
     * @return
     */
    @GetMapping("findAddressByUserId")
    public UserResult<List<Address>> findAddressByUserId(int userId){
        if (userId>0){
            return addressService.findAddressByUserId(userId);
        }
        return UserResult.failed();
    }

    /**
     *修改收货地址
     *
     * @param addressDto
     * @return
     */
    @PostMapping("updateAddress")
    public UserResult<Boolean> updateAddress(AddressDto addressDto){

        UserResult<Boolean> byUserId = addressService.updateAddress(addressDto);
        return byUserId;
    }

    /**
     * 添加收货地址
     *
     * @param addressDto 收货地址信息
     * @return
     */
    @PostMapping("addAddress")
    public UserResult<Long> addAddress(AddressDto addressDto){
        UserResult<Long> booleanUserResult = addressService.addAddress(addressDto);
        return booleanUserResult;
    }


    /**
     * 修改默认收货地址
     *
     * @param addressDefaultDto
     * @return
     */
    @PostMapping("updateAddressDefault")
    public UserResult<String> updateAddressDefault(AddressDefaultDto addressDefaultDto){

        return addressService.updateAddressDefault(addressDefaultDto);
    }

    /**
     * 删除收货地址
     *
     * @param id
     * @return
     */
    @PostMapping("delAddress")
    public UserResult<String> delAddress(long id){
        return addressService.delAddress(id);
    }


}
